package com.teacher.mobile.controller;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.common.utils.HttpClientUtil;
import com.teacher.pojo.dto.teacher.TeacherUpdateDto;
import com.teacher.pojo.dto.wx.WechatUserUpdateDto;
import com.teacher.pojo.dto.wx.WxLoginDto;
import com.teacher.pojo.dto.wx.WxSignDto;
import com.teacher.pojo.vo.WxLoginVo;
import com.teacher.pojo.vo.teacher.LoginVo;
import com.teacher.security.entity.annotation.WhiteApi;
import com.teacher.service.IWechatUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author
 * @since 2023-04-13
 */
@RestController
@RequestMapping("/wx")
@Api(tags = "微信小程序登录接口")
public class WechatUserController {

    @Autowired
    private IWechatUserService iWechatUserService;



    @PostMapping("/wxLogin")
    @ResponseBody
    @ApiOperation("用户授权登录")
    @WhiteApi
    public CommonResult<WxLoginVo> wxLogin(@RequestBody WxLoginDto userLoginVo) throws Exception {
        return iWechatUserService.wxLogin(userLoginVo);
    }

    @PostMapping("/wxLoginTest")
    @ResponseBody
    @ApiOperation("测试用模拟拿到openid后登录获取token,需要数据库已存在此用户")
    @WhiteApi
    public CommonResult<WxLoginVo> wxLoginTest(String openId) throws Exception {
        return iWechatUserService.wxLoginTest(openId);
    }

    /**
     * 更新信息
     *
     * @param dto
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation("后台更新用户信息")
    public CommonResult<String> updateUser(@RequestBody WechatUserUpdateDto dto) {
        return iWechatUserService.updateInfo(dto);
    }

    /**
     * 修改自己手机号
     *
     * @param phoneNumber
     * @return
     */
    @PutMapping("/updatePhoneNumber")
    @ApiOperation("更新当前登录用户的手机号")
    public CommonResult<String> updatePhoneNumber(String phoneNumber) {
        return iWechatUserService.updatePhoneNumber(phoneNumber);
    }

    /**
     * 修改自己学校
     *
     * @param school
     * @return
     */
    @PutMapping("/updateSchool")
    @ApiOperation("更新当前登录用户的学校")
    public CommonResult<String> updateSchool(String school) {
        return iWechatUserService.updateSchool(school);
    }

    /**
     * 修改自己昵称
     *
     * @param name
     * @return
     */
    @PutMapping("/updateName")
    @ApiOperation("更新当前登录用户的昵称")
    public CommonResult<String> updateName(String name) {
        return iWechatUserService.updateName(name);
    }


    /**
     * 修改自己头像
     *
     * @param imgUrl
     * @return
     */
    @PutMapping("/updateImgUrl")
    @ApiOperation("更新当前登录用户的头像url")
    public CommonResult<String> updateImgUrl(String imgUrl) {
        return iWechatUserService.updateImgUrl(imgUrl);
    }

    /**
     * 注册时的信息
     *
     * @param wxSignDto
     * @return
     */
    @PutMapping("/signInfo")
    @ApiOperation("注册时更新用户信息")
    public CommonResult<String> signInfo(WxSignDto wxSignDto) {
        return iWechatUserService.signInfo(wxSignDto);
    }

}